package com.mall.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.exception.MyException;
import com.mall.model.Order;
import com.mall.model.User;
import com.mall.service.OrderService;
import com.mall.util.MyUtil;

@Controller
@RequestMapping("/OrderController")
public class OrderController extends MyUtil {
	@Autowired
	private OrderService orderService;

	/**
	 * 添加订单
	 *
	 * @dagewang 2017年9月6日
	 */
	@RequestMapping("/addOrder")
	public @ResponseBody Map<String, Object> addOrder(Order order, Integer seckill) throws Exception {
		int result = orderService.save(order, seckill);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		return map;
	}

	/**
	 * 获取订单集合
	 * 
	 * @dagewang 2017年8月16日
	 */
	@RequestMapping("/listOrder")
	public String listOrder(Model modle, Integer page, Integer state) throws Exception {
		if (state == null || state.equals("")) {
			throw new MyException("出现了错误请刷新页面");
		}
		if (page == null) {
			page = 0;
		}
		modle.addAttribute("page", page);
		List<Order> list = orderService.listOrder(state, page);
		if (list.size() == 0) {
			modle.addAttribute("orders", null);
		} else {
			modle.addAttribute("orders", list);
		}
		if (state == 1) {
			return "deliveryOrder";
		} else if (state == 2) {
			return "alreaDydeliveryOrder";
		}

		return "newOrder";
	}

	/**
	 * 获取订单集合,所有订单
	 * 
	 * @dagewang 2017年8月16日
	 */
	@RequestMapping("/allOrder")
	public String allOrder(Model modle, Integer page, Integer userid) throws Exception {
		if (page == null) {
			page = 0;
		}
		modle.addAttribute("page", page);
		if (userid != null && !userid.equals("")) {
			List<Order> list = orderService.listOrder(page);
			if (list.size() == 0) {
				modle.addAttribute("orders", null);
			} else {
				modle.addAttribute("orders", list);
			}
			return "userCenterOrder";
		} else {
			List<Order> list = orderService.listOrder(page);
			if (list.size() == 0) {
				modle.addAttribute("orders", null);
			} else {
				modle.addAttribute("orders", list);
			}
		}
		return "allOrder";
	}

	/**
	 * 根据用户id获取所有订单
	 * 
	 * @dagewang 2017年8月16日
	 */
	@RequestMapping("/allOrderByUserId")
	public String allOrderbyUserid(HttpServletRequest hsr, Model modle, Integer page, HttpSession session,
			Integer state) throws Exception {
		String ip = "";
		ip = hsr.getHeader("X-FORWARDED-FOR");
		System.out.println(ip);
		if (ip == null || ip.equals("")) {
			ip = hsr.getRemoteAddr();
		}
		System.out.println(ip);
		if (state == null || state.equals("")) {
			state = 0;
		}
		if (page == null || page < 0) {
			page = 0;
		}
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "login";
		}
		modle.addAttribute("page", page);
		List<Order> list = orderService.listOrderByUserId(user.getId(), page, state);
		if (list.size() == 0) {
			modle.addAttribute("orders", null);
		} else {
			modle.addAttribute("orders", list);
		}
		modle.addAttribute("state", state);
		return "userCenterOrder";
	}

	/**
	 * 查看是否有订单
	 * 
	 * @dagewang 2017年8月16日
	 */
	@RequestMapping("/listOrderAjax")
	public @ResponseBody Map<String, Object> listOrderAjax() throws Exception {
		boolean listOrder = orderService.listOrder();
		Map<String, Object> map = new HashMap<String, Object>();
		if (listOrder) {
			map.put("bl", "bl");
		} else {
			map.put("bl", null);
		}

		return map;
	}

	/**
	 * 修改订单状态
	 * 
	 * @throws Exception
	 * @dagewang 2017年8月16日
	 */
	@RequestMapping("/updateOrder")
	public void updateOrder(Integer id, Integer state, String remarks, Integer staffid) throws Exception {
		if (this.isNull(id, state)) {
			throw new MyException("出现了内部错误,无法更新订单");
		}
		int i = 0;
		if (staffid != null) {
			i = orderService.update(id, state, staffid);
		}
		if (remarks != null && !remarks.equals("")) {
			i = orderService.update(id, state, remarks,null);
		} else {
			i = orderService.update(id, state);
		}
		if (i == 0) {
			throw new MyException("出现了内部错误,无法更新订单");
		}
	}
	@RequestMapping("/updateToStaff")
	public @ResponseBody Map<String,Object> updateToStaff(Integer state,String transport,Integer id){
		Map<String,Object> map=new HashMap<String,Object>();
			if(this.isNull(state,id)){
				map.put("u", 0);
			}else{
				int update=0;
				if(transport==null||transport.equals("")){
					update= orderService.update(id, state);
				}else{
					update= orderService.update(id, state,transport, 1);
				}
				map.put("u", update);
			}
			return map;
	}

	/**
	 * 根据id取订单
	 * 
	 * @throws MyException
	 *
	 * @dagewang 2017年8月19日
	 */
	@RequestMapping("/get")
	public @ResponseBody Map<String, Object> get(Integer id) throws MyException {
		if (this.isNull(id)) {
			throw new MyException("is error");
		}
		Order order = orderService.get(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("getorder", order);
		return map;
	}

	/**
	 * 删除订单Byid
	 *
	 * @dagewang 2017年9月12日
	 */
	@RequestMapping("/delete")
	public @ResponseBody Integer delete(Integer id) throws Exception {
		if (id == null || id.equals("")) {
			return 0;
		}
		return orderService.delete(id);
	}

	/**
	 * 客户取消订单
	 * 
	 * @throws Exception
	 *
	 * @dagewang 2017年9月12日
	 */
	@RequestMapping("cancel")
	public @ResponseBody Integer cancel(Integer id, String content) throws Exception {
		if (this.isNull(id, content)) {
			return 0;
		}
		Order order = orderService.cancel(id, content);
		if (order == null) {
			return 0;
		}
		return 1;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

}
